package com.job2e.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.code.jbauble.mvc.Controller;
import com.google.code.jbauble.mvc.Result;
import com.google.code.jbauble.mvc.annotations.RequestMapping;
import com.google.code.jbauble.mvc.support.UrlHelper;
import com.job2e.common.BaseController;
import com.job2e.model.Hr;
import com.job2e.model.User;
import com.job2e.service.HrService;
import com.job2e.service.UserService;

public class HrController extends BaseController {
	private HrService service = null;
	public HrController() {
		service = (HrService)getBean("hrService");
	}
	
	public Result index() {
		return render();
	}
	
	private final String hrSession = "hr"; 
			
	@RequestMapping(method = UrlHelper.HTTP_GET)
	public Result login() {
		return render();
	}
	
	@RequestMapping(method = UrlHelper.HTTP_POST)
	public Result login(String email, String password, HttpServletRequest request) {
		Hr u = service.login(email, password);
		if (u != null) {
			request.getSession().setAttribute(hrSession, u);
			return output("0");
		}
		
		return output("-1");
	}
	
	public Result logout(HttpServletRequest request, HttpServletResponse response) {
		request.getSession().setAttribute(hrSession, null);
		return redirect("hr/login");
	}
	
	@RequestMapping(method = UrlHelper.HTTP_GET)
	public Result register() {
		return render();
	}
	
	@RequestMapping(method = UrlHelper.HTTP_POST)
	public Result register(Hr hr) {
		service.save(hr);
		if (hr.getHrId() != null) {
			return output("0");
		}
		return output("-1");
	}
}
